package com.common.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.common.R;
import com.common.utility.CommonUtility;

public class LoadingBar extends RelativeLayout{

//	private Context context;
	private Thread animationThread;
	private boolean stopped = true;
	private int currentStep = 0;
	private int count = 7;
	private ImageView logoIv;
	private float dotHeight ;
	private float progressWidth ;
	private float topMargin;
	private float progressLeft;
	private float progressTop;
	
	public LoadingBar(Context context) {
		super(context.getApplicationContext());
//		this.context = context.getApplicationContext();
		this.setEnabled(false);
		this.setBackgroundColor(Color.BLACK);
		int width = CommonUtility.UIUtility.getScreenWidth(context);
        int height = CommonUtility.UIUtility.getScreenHeight(context);
        progressWidth = (float)width/ 5.3f;
        dotHeight = height/42.7f;
        topMargin = height/26.7f;
		prepareLayout();
		
	}
	
	public LoadingBar(Context context, AttributeSet attrs) {
	    super(context, attrs);
	}
	
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
    	return false;
    }
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent pEvent) {
		return true;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return false;
	}
	
    @Override 
    protected void onDraw(Canvas canvas) {
    	
//    	for(int i=0;i<count;i++){
//    		this.drawRect(canvas, i);
//    	}
    	invalidate();
    }
    
    private void drawRect(Canvas canvas,int index){
  	  Paint paint = new Paint();
  	  paint.setColor(this.getColor(index));
  	  paint.setStyle( Style.FILL );
  	  
	  progressLeft = getWidth()/2f - progressWidth/2f;
	  progressTop = getHeight()/2f + topMargin;
		
  	  float left ;
  	  float top ;
  	  float right ;
  	  float bottom ;
  	  
      if (currentStep == index) {
    	  left =progressLeft + progressWidth*index/8.0f;
    	  top = progressTop;
    	  right = left + progressWidth/(count*2+1);
    	  bottom = top + dotHeight;
      } else if (currentStep < index) {
    	  left =progressLeft + progressWidth * index/8.0f;
    	  top = progressTop + dotHeight / 5.0f;
    	  right = left + progressWidth / (count*2+1);
    	  bottom = top + dotHeight * 3.0f / 5.0f;
      } else {
          if (currentStep - index == 1) {
        	  left = progressLeft + progressWidth * index / 8.0f;
        	  top = progressTop + dotHeight / 10.0f;
        	  right = left + progressWidth / (count*2+1);
        	  bottom = top + dotHeight * 4.0f / 5.0f;
        	  
          } else {
        	  left = progressLeft + progressWidth * index / 8.0f;
        	  top = progressTop + dotHeight / 5.0f;
        	  right = left + progressWidth / (count*2+1);
        	  bottom = top + dotHeight * 3.0f/5.0f;
          }
      }
  	  canvas.drawRect( left,top,right,bottom, paint );
    }
    
    private int getColor(int index){
        if (currentStep == index) {
        	return Color.rgb(0, 150, 200);
        } else if (currentStep < index) {
            return Color.alpha(0);
        } else {
            if (currentStep - index == 1) {
            	return Color.rgb(0, 150, 200);
            } else {
            	return Color.rgb(255, 255, 255);
            }
        }        
    }

	/**
	 * This is called when you want the dialog to be dismissed
	 */
	public void dismiss() {
//		stopped = false;
//		if(animationThread != null ){
//			animationThread.interrupt();
//	    }
//		setVisibility(View.GONE);
	}

	/**
	 * Loads the layout and sets the initial set of images
	 */
	@SuppressLint("ResourceAsColor")
	private void prepareLayout() {

//		logoIv = new ImageView(this.getContext());
//		logoIv.setImageResource(R.drawable.car_loading_icon);// project_logo 图片
//		logoIv.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
		
		LayoutInflater inflater = LayoutInflater.from(this.getContext());
		View progressBar = inflater.inflate(R.layout.progressbar_layout, null);
		
//		View view = new View(getContext());
//		int  wight = AppApplication.getInstance().getDisplayHightAndWightPx()[1]/3;
//		int  height = AppApplication.getInstance().getDisplayHightAndWightPx()[1]/4;
//		view.setBackgroundColor(android.R.color.black);
//		view.setAlpha(204);
//		addView(view,new RelativeLayout.LayoutParams(wight, height));
		
		addView(progressBar,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		this.setGravity(Gravity.CENTER);
	}

	/**
	 * Starts the animation thread
	 */
	public void startAnimation() {
//		setVisibility(View.VISIBLE);
//		animationThread = new Thread(new Runnable(){
//			@Override
//			public void run() {
//				while (stopped) {
//					try {
//						Log.d("", "---------------");
//						Thread.sleep(50);
//						handler.sendEmptyMessage(0);
//					} catch (InterruptedException e) {
//						e.printStackTrace();
//						Thread.currentThread().interrupt();
//					}
//				}				
//			}}, "Progress");
//		animationThread.start();
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			 currentStep = ++currentStep % (count*2+1);
			super.handleMessage(msg);
		}

	};

}